package com.sdp.right.control;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.redis.RedisUtil;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.right.cache.keys.RightCacheKeys;
import com.sdp.right.db.sys_dr_role_rule.service.ISysDrRoleRuleService;
import com.sdp.right.entity.SysDrRoleRule;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2023-03-28
 */
@RestController
@RequestMapping("/SysDrRoleRuleController")
public class SysDrRoleRuleController {
	
	@Autowired
	private ISysDrRoleRuleService iSysDrRuleService;
	
	@Autowired
	private Query query;
	
	@Autowired
	private RedisUtil redisUtil;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iSysDrRuleService, SysDrRoleRule.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional
	public Result<?> save(@RequestBody SysDrRoleRule sysDrRoleRule){
		if(sysDrRoleRule==null) {
			return Result.error("数据权限规则信息为空");
		}
		
		if(StringUtils.isEmpty(sysDrRoleRule.getKeyId())) {
			iSysDrRuleService.save(sysDrRoleRule);
		}else {
			iSysDrRuleService.updateById(sysDrRoleRule);
			//删除缓存
			redisUtil.del(RightCacheKeys.DR_ROLE_CACHE_KEY);
		}
	
		return Result.ok(sysDrRoleRule);
	}
	
	@GetMapping(value = "/del")
	@Transactional
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("需要删除的记录的主键不存在");
		}
		iSysDrRuleService.removeById(keyId);
		//删除缓存
		redisUtil.del(RightCacheKeys.DR_ROLE_CACHE_KEY);
		return Result.ok();
	}
}

